package com.ruoyi.customer.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.customer.domain.TMesCustomer;
import com.ruoyi.customer.service.ITMesCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 客户Controller
 *
 * @author ruoyi
 * @date 2024-05-26
 */
@RestController
@RequestMapping("/customer/customer")
@Api(tags = "客户")
public class TMesCustomerController extends BaseController {
    @Autowired
    private ITMesCustomerService tMesCustomerService;

    /**
     * 查询客户列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody TMesCustomer tMesCustomer) {
        startPage();
        List<TMesCustomer> list = tMesCustomerService.selectTMesCustomerList(tMesCustomer);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody TMesCustomer tMesCustomer) {
        startOrderBy();
        List<TMesCustomer> list = tMesCustomerService.selectTMesCustomerList(tMesCustomer);
        return success(list);
    }

    /**
     * 导出客户列表
     */
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, TMesCustomer tMesCustomer) {
        List<TMesCustomer> list = tMesCustomerService.selectTMesCustomerList(tMesCustomer);
        ExcelUtil<TMesCustomer> util = new ExcelUtil<TMesCustomer>(TMesCustomer.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(tMesCustomerService.selectTMesCustomerById(id));
    }

    /**
     * 新增客户
     */
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody TMesCustomer tMesCustomer) {
        SysUser user = getLoginUser().getUser();
        if(tMesCustomer.getId() == null) {
            tMesCustomer.setCreateId(user.getUserId());
            tMesCustomer.setCreateBy(user.getNickName());
            tMesCustomer.setCreateTime(DateUtils.getNowDate());
            tMesCustomer.setDeleteFlag(0);

            String prefix = "KH";
            if("2".equals(tMesCustomer.getType())) {
                prefix = "GYS";
            }

            tMesCustomer.setNo(tMesCustomerService.generNo(prefix, "t_mes_customer"));
            tMesCustomerService.insertTMesCustomer(tMesCustomer);
        } else {
            tMesCustomer.setUpdateId(user.getUserId());
            tMesCustomer.setUpdateTime(DateUtils.getNowDate());
            tMesCustomerService.updateTMesCustomer(tMesCustomer);
        }

        return success(tMesCustomer);
    }


    /**
     * 修改客户
     */
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody TMesCustomer tMesCustomer) {
        SysUser user = getLoginUser().getUser();
        tMesCustomer.setUpdateId(user.getUserId());
        tMesCustomer.setUpdateTime(DateUtils.getNowDate());
        return success(tMesCustomerService.updateTMesCustomer(tMesCustomer));
    }

    /**
     * 删除客户
     */
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tMesCustomerService.deleteTMesCustomerByIds(ids));
    }

    @Log(title = "客户", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody TMesCustomer tMesCustomer) {
        int num = tMesCustomerService.delete(tMesCustomer);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<TMesCustomer>> queryPage(@RequestBody TMesCustomer tMesCustomer) {
        Pages<TMesCustomer> page = tMesCustomerService.queryPage(tMesCustomer);
        return ResponseModel.of(page);
    }
}
